package com.fingard.dsp.bank.directbank.cdbank01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextWriter;

/**
 * @author: liuxb
 * @create: 2020-01-02 11:49
 * @version: 1.0
 **/
public class CDBANK01Balance extends CDBANK01Base  implements IBalance {

    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        //余额查询返回ATS的对象
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            CDBANK01Header bankHeader = new CDBANK01Header();
            bankHeader.serviceId = "CMACCBAL";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeStartElement("list");
//            XElement list = new XElement("list", "");
//                list.addAttr("name", "userDataList");
//            xmlWriter.writeXElement(list);
                while (p_req7506.readRecord()) {
                    xmlWriter.writeStartElement("row");
                    xmlWriter.writeElementString("ACCNO", p_req7506.readedDetail.actNum);//账号
                    xmlWriter.writeElementString("CRYTYPE", convertCurToBank(p_req7506.readedDetail.actCur));//币种
                    xmlWriter.writeEndElement();
                }
                xmlWriter.writeEndDocument();
                // 发送给银行获得应答报文
//                String[] resultBank = sendToBank(xmlWriter.toXmlString(), bankHeader);
                //模拟银行返回
            String s = "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
                    "<stream>\n" +
                    "<RETCODE>AAAAAAA</RETCODE>\n" +
                    "<RETMSG>状态描述</RETMSG>\n" +
                    "<list>\n" +
                    "\t<row>\n" +
                    "\t\t<ACCNO>12121</ACCNO>\n" +
                    "\t\t<ACCTYPE>账户类型</ACCTYPE>\n" +
                    "<ACCTYPEDESC>账户类型描述</ACCTYPEDESC>\n" +
                    "\t\t<ACCNM>账户名称</ACCNM>\n" +
                    "\t\t<ACCBAL>1200.88</ACCBAL>\n" +
                    "\t\t<ACCAMT>1120.66</ACCAMT>\n" +
                    "\t\t<CRYTYPE>CNY</CRYTYPE>\n" +
                    "\t\t    <RETURNCODE>AAAAAAA</RETURNCODE>\n" +
                    "\t\t<RETURNMSG>返回说明</RETURNMSG>\n" +
                    "\t</row>\n" +
                    "</list>\n" +
                    "</stream>\n";
            String[] resultBank = new String[]{"", s};
                if (resultBank[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(resultBank[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/stream/RETCODE");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/stream/RETMSG");
                        if ("AAAAAAA".equals(IBSReturnCode)) {
                            while (tmpBkReader.read("/stream/list/row")) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("ACCNO");//帐号
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("ACCBAL");//帐户余额
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("ACCAMT");//可用余额
                                tmpRetDetail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("CRYTYPE"));
                                String returnCode = tmpBkReader.readedXElement.getSingleText("RETURNCODE");
                                tmpRetDetail.respCode = "AAAAAAA".equals(returnCode) ? "0" : "-1";
                                tmpRetDetail.respInfo = tmpBkReader.readedXElement.getSingleText("RETURNMSG");
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);// 把信息提取完毕的对象装载入返回给ats的对象中
                            }

                        } else if (IBSReturnCode != null && "AAAAAAE".equals(IBSReturnCode)) {// 已提交银行处理，需稍后使用“交易状态查询”交易进行查询
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("已提交银行处理，需稍后使用“交易状态查询”交易进行查询");
                        }else if (IBSReturnCode != null && "UNKNOWN".equals(IBSReturnCode)) {// 交易状态未知，使用“交易状态查询”交易进行查询
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo("交易状态未知，使用“交易状态查询”交易进行查询");
                        }  else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(IBSReturnMsg);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                }else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(resultBank[1]);
                }
        }catch (Exception e) {
            retAts = new RetAts7506BalQry(p_req7506.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
